package com.jsmy.marketing.utils;

import android.app.Activity;

import java.util.LinkedList;
import java.util.List;

/**
 * Created by YuLiang on 2017/2/22.
 */

public class ActivityManager {

    private List<Activity> activityList = new LinkedList<Activity>();
    private static ActivityManager instance;

    private ActivityManager() {
    }

    // 单例模式中获取唯一的MyApplication实例
    public static ActivityManager getInstance() {
        if (null == instance) {
            instance = new ActivityManager();
        }
        return instance;
    }

    // 添加Activity到容器中
    public void addActivity(Activity activity) {
        activityList.add(activity);
    }

    // 遍历所有Activity并finish
    public void exit() {
        for (Activity activity : activityList) {
            activity.finish();
        }
        System.exit(0);
    }
    // 移除指定的activity
    public void exitActivity(Activity activity1) {
        if (activityList.contains(activity1)){
            int i = activityList.indexOf(activity1);
            Activity activity = activityList.get(i);
            activity.finish();
        }
    }
    /**
     * 结束指定的Activity
     */
    public  void finishActivity(Activity activity) {
        if (activityList == null||activityList.isEmpty()) {
            return;
        }
        if (activity != null) {
            activityList.remove(activity);
            activity.finish();
            activity = null;
        }
    }


    /**
     * 按照指定类名找到activity
     *
     * @param cls
     * @return
     */
    public  Activity findActivity(Class<?> cls) {
        Activity targetActivity = null;
        if (activityList != null) {
            for (Activity activity : activityList) {
                if (activity.getClass().equals(cls)) {
                    targetActivity = activity;
                    break;
                }
            }
        }
        return targetActivity;
    }

    /**
     * @return 作用说明 ：获取当前最顶部activity的实例
     */
    public Activity getTopActivity() {
        Activity mBaseActivity = null;
        synchronized (activityList) {
            final int size = activityList.size() - 1;
            if (size < 0) {
                return null;
            }
            mBaseActivity = activityList.get(size);
        }
        return mBaseActivity;

    }

    /**
     * @return 作用说明 ：获取当前最顶部的acitivity 名字
     */
    public String getTopActivityName() {
        Activity mBaseActivity = null;
        synchronized (activityList) {
            final int size = activityList.size() - 1;
            if (size < 0) {
                return null;
            }
            mBaseActivity = activityList.get(size);
        }
        return mBaseActivity.getClass().getName();
    }

    // 遍历所有Activity并finish
    public void finishAll() {
        for (Activity activity : activityList) {
            activity.finish();
        }
    }
    // 遍历所有Activity并finish
    public void finishAllExcept(Class<?> context) {
        for (Activity activity : activityList) {
            if (activity.getClass()!=context){
                activity.finish();
            }
        }
    }
}
